/*************************************************************************
> File Name: stack.c
> Author: icebear
> Mail: 3322366195@qq.com
> Created Time: 2023-07-24 19:01:41
> Description: 数组实现栈
************************************************************************/
#include "stack.h"
#include <stdio.h>
#include <stdlib.h>
#define SIZE (200)
int main(int argc, char const *argv[])
{
    printf("create: \n");
    Stack *stack = create();
    printf("push: 1-9\n");
    for (int i = 0; i < 10; i++)
    {
        push(stack, i);
    }
    printf("size: %d\n", size(stack));
    printf("isEmpty: %d\n", isEmpty(stack));
    printf("peek: %d\n", peek(stack));
    printf("pop:10 item \n");
    for (int i = 0; i < 11; i++)
    {
        printf("%3d", pop(stack));
    }
    printf("\n");
    printf("isEmpty: %d\n", isEmpty(stack));
    printf("size: %d\n", size(stack));
    printf("\n");
    return 0;
}

Stack *create()
{
    Stack *stack = malloc(sizeof(Stack));
    stack->p = malloc(sizeof(int) * SIZE);
    stack->size = 0;
    return stack;
}
int destory(Stack *stack)
{
    if (stack != NULL)
    {
        free(stack->p);
        if (stack->p != NULL)
        {
            return -1;
        }
        free(stack);
        if (stack != NULL)
        {
            return -1;
        }
    }
    return 1;
}
// 入栈
void push(Stack *stack, int item)
{
    *((stack->p) + stack->size) = item;
    stack->size++;
}
// 出栈
int pop(Stack *stack)
{
    if (stack->size != 0)
    {
        int item = *((stack->p) + stack->size - 1);
        stack->size--;
        return item;
    }
    return -1;
}
// 返回栈顶元素不出栈
int peek(Stack *stack)
{
    return *((stack->p) + stack->size - 1);
}
int isEmpty(Stack *stack)
{
    if (stack->size == 0)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}
int size(Stack *stack)
{
    return stack->size;
}
